package com.gl.emms.client.android;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import com.gl.emms.nio.constant.EMMSConstant;
import com.gl.emms.nio.mutual.ServerMessage;
import com.gl.emms.nio.mutual.ReplyBody;
import com.gl.emms.nio.mutual.RequestBody;
import com.gl.emms.nio.mutual.SentBody;
/**
 *  消息入口，所有消息都会经过这里
 * @author 3979434
 *
 */
public  abstract  class EMMSEnventListenerReceiver extends BroadcastReceiver implements OnEMMSMessageListener {
 

	public Context context;
	@Override
	public void onReceive(Context ctx, Intent it) {

		  context = ctx;
		  Log.v("whale", "CIMEnventListenerReceiver action:"+it.getAction());
		  if(it.getAction().equals(EMMSConnectorManager.ACTION_NETWORK_CHANGED))
          {
			  Log.v("whale", "ACTION_NETWORK_CHANGED");
			  ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService("connectivity");
	          android.net.NetworkInfo info = connectivityManager.getActiveNetworkInfo();
	        
	          onDevicesNetworkChanged(info);
          }
		
          if(it.getAction().equals(EMMSConnectorManager.ACTION_CONNECTION_CLOSED))
          {
        	  Log.v("whale", "ACTION_CONNECTION_CLOSED");
        	  onConnectionClosed();
          }
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_SEND_HEARTBEAT))
          {
        	  Log.v("whale", "ACTION_SEND_HEARTBEAT");
        	  onSendHeartBeat();
          }
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_CONNECTION_FAILED))
          {
        	  Log.v("whale", "ACTION_CONNECTION_FAILED");
        	  onConnectionFailed((Exception) it.getSerializableExtra("exception"));
          }
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_CONNECTION_SUCCESS))
          {
        	  Log.v("whale", "ACTION_CONNECTION_SUCCESS");
        	  onDispatchConnectionSucceed();
          }
         
          if(it.getAction().equals(EMMSConnectorManager.ACTION_MESSAGE_RECEIVED))
          {
        	  Log.v("whale", "ACTION_MESSAGE_RECEIVED");
        	  filterType999Message((ServerMessage)it.getSerializableExtra("message"));
          }
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_REPLY_RECEIVED))
          {
        	  Log.v("whale", "ACTION_REPLY_RECEIVED");
        	  onReplyReceived((ReplyBody)it.getSerializableExtra("replyBody"));
          }
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_REQUEST_RECEIVED))
          {
        	  Log.v("whale", "ACTION_REQUEST_RECEIVED");
        	  onRequestReceived((RequestBody)it.getSerializableExtra("requestBody"));
          }
          
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_SENT_FAILED))
          {
        	  Log.v("whale", "ACTION_SENT_FAILED");
        	  onSentFailed((Exception) it.getSerializableExtra("exception"),(SentBody)it.getSerializableExtra("sentBody"));
          }
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_SENT_SUCCESS))
          {
        	  Log.v("whale", "ACTION_SENT_SUCCESS");
        	  onSentSucceed((SentBody)it.getSerializableExtra("sentBody"));
          }

          if(it.getAction().equals(EMMSConnectorManager.ACTION_UNCAUGHT_EXCEPTION))
          {
        	  Log.v("whale", "ACTION_UNCAUGHT_EXCEPTION");
        	  onUncaughtException((Exception)it.getSerializableExtra("exception"));
          }
          
          if(it.getAction().equals(EMMSConnectorManager.ACTION_CONNECTION_STATUS))
          {
        	  Log.v("whale", "ACTION_CONNECTION_STATUS");
        	  onConnectionStatus(it.getBooleanExtra(EMMSPushManager.KEY_CIM_CONNECTION_STATUS, false));
          }
          
          
	}

	protected boolean isInBackground(Context context) {
		List<RunningTaskInfo> tasksInfo = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningTasks(1);
		if (tasksInfo.size() > 0) {

			if (context.getPackageName().equals(
					tasksInfo.get(0).topActivity.getPackageName())) {

				return false;
			}
		}
		return true;
	}

	private  void onConnectionClosed(){
		
		//是否后台关闭
		//boolean  isManualStop  = EMMSDataConfig.getBoolean(context,EMMSDataConfig.KEY_MANUAL_STOP);
		
		//boolean  isManualDestory  = EMMSDataConfig.getBoolean(context,EMMSDataConfig.KEY_CIM_DESTORYED);
		
		//if(EMMSConnectorManager.netWorkAvailable(context) && !isManualStop && !isManualDestory)
		if(EMMSConnectorManager.netWorkAvailable(context))
		{
			EMMSPushManager.init(context);
		}
	}

	private   void onConnectionFailed(Exception e){
		if(EMMSConnectorManager.netWorkAvailable(context))
		{
			EMMSPushManager.init(context);
		}
	}

	 
	private void onUncaughtException(Throwable arg0) {}

	

	private  void onDevicesNetworkChanged(NetworkInfo info) {
		
		if(info !=null)
		{
			EMMSPushManager.init(context);
		} 
		
		onNetworkChanged(info);
	}

	private void filterType999Message(com.gl.emms.nio.mutual.ServerMessage message)
	{
		Log.v("whale", "filterType999Message");
		if(EMMSConstant.MessageType.TYPE_999.equals(message.getType()))
		{
			EMMSDataConfig.putBoolean(context,EMMSDataConfig.KEY_MANUAL_STOP,true);
		}
		
		onMessageReceived(message);
	}
	
	private   void onSentFailed(Exception e, SentBody body){
		
		//与服务端端开链接，重新连接
		if(e instanceof EMMSSessionDisableException)
		{
			EMMSPushManager.init(context);
		}else
		{
			//发送失败 重新发送
			EMMSPushManager.sendRequest(context, body);
		}
		
	}

	
    private  void  onDispatchConnectionSucceed(){
		
		//CIMPushManager.setAccount(context);
		//CIMDataConfig.putBoolean(context,CIMDataConfig.KEY_MANUAL_STOP,false);
    	EMMSPushManager.setID(context);
		onConnectionSucceed();
	}


	private  void onSentSucceed(SentBody body){}
	
	@Override
	public abstract void onMessageReceived(com.gl.emms.nio.mutual.ServerMessage message);
	@Override
	public abstract void onReplyReceived(ReplyBody body);
	
	public abstract void onNetworkChanged(NetworkInfo info);
	
	@Override
	public abstract void onSendHeartBeat();
	
}
